package manage.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import manage.dao.WageMapper;
import manage.model.Pay;
import manage.model.Wage;
import manage.util.ExcelUtil;
import manage.util.PayUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.*;

@Service
public class WageServiceImpl {
    @Autowired
    private WageMapper wageMapper;
    public PageInfo<Wage> selectAll(String page, String limit) {
        int pu = Integer.parseInt(page);
        int li = Integer.parseInt(limit);
        PageHelper.startPage(pu,li);
        List<Wage> list = wageMapper.selectAll();
        PageInfo<Wage> pageInfo = new PageInfo<Wage>(list);
        return pageInfo;
    }
    public String insert(Wage record) {
        int i = wageMapper.insert(record);
        if(i>0)
            return "success";
        return "fail";
    }
    public String del(Integer id) {
        int i = wageMapper.deleteByPrimaryKey(id);
        if(i>0)
            return "success";
        return "fail";
    }
    public Wage select(Integer id) {
        return wageMapper.selectByPrimaryKey(id);
    }
    public String update(Wage record){
        if(wageMapper.updateByPrimaryKey(record)>0)
            return "success";
        return "fail";
    }

    @Autowired
    private PayServiceImpl payService;
    @Autowired
    private  RatesServiceImpl ratesService;

    public String InputExcel(InputStream is, String originalFilename) {
        List<ArrayList<Object>> list;
        if (originalFilename.endsWith(".xls")) {
            list = ExcelUtil.readExcel2003(is);
        } else {
            list = ExcelUtil.readExcel2007(is);
        }
        for (int i = 0 ; i < list.size(); i++) {
            List<Object> row = list.get(i);
            Wage wage = new Wage();
            wage.setAccount(row.get(0).toString());
            wage.setUsername(row.get(1).toString());
            wage.setDepartments(row.get(2).toString());
            wage.setBsalary(row.get(3).toString());
            wage.setTardinesspenalty(Double.parseDouble(row.get(4).toString()));
            wage.setEarlyretirementpenalty(Double.parseDouble(row.get(5).toString()));
            wage.setPerformancereward(Double.parseDouble(row.get(6).toString()));
            wage.setOvertimereward(Double.parseDouble(row.get(7).toString()));
            wage.setAllowanceforhightemperature(Double.parseDouble(row.get(8).toString()));
            wage.setTrafficsubsidy(Double.parseDouble(row.get(9).toString()));
            wage.setRentalsubsidy(Double.parseDouble(row.get(10).toString()));
            wage.setPremium(row.get(11).toString());
            Pay pay = new Pay();
            pay.setAccount(wage.getAccount());
            pay.setUsername(wage.getUsername());
            double base = Integer.parseInt(wage.getBsalary())+Integer.parseInt(wage.getPremium())
                    +wage.getAllowanceforhightemperature()-wage.getEarlyretirementpenalty()
                    +wage.getOvertimereward()+wage.getPerformancereward()
                    -wage.getTardinesspenalty()+wage.getRentalsubsidy()+wage.getTrafficsubsidy();
            pay.setBasewage(base+"");
            double pay01 = base-base* PayUtil.readProperty2()/100;
            pay01 = (double) Math.round(pay01 * 100) / 100;
            pay.setPaywage(pay01+"");
            pay.setBooleanpay("否");
            pay.setPaytime(new Date());
            pay.setAt(ratesService.getMoney((int)pay01));
            payService.insert(pay);
            wageMapper.insert(wage);
        }
        return "01";
    }

    public List<Wage> selectByAccount(String account) {
        return wageMapper.selectByAccount(account);
    }
}
